package beans;

import utils.EmailSender;
import utils.GetBeanUtil;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "sendLinkBean")
@ViewScoped
public class SendLinkBean {
    private static final String SUBJECT = "Link to download the file from FILESHARE";

    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void sendLink() {
        try {
            DocumentsBean documentsBean = (DocumentsBean) GetBeanUtil.getBean("documentsBean");
            EmailSender.sendEmail(email, SUBJECT, documentsBean.getDocument().getLink());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Your message has been sent!"));
        }
        catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", e.toString()));
        }
    }
}
